package com.gc.client;

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;

import com.gc.model.GcCommunicationModel;

public class GcFileClient {
	public final static int SOCKET_PORT = 7878;
	public final static String SERVER = "203.233.194.231";
	public final static int FILE_SIZE = 1024786;
	public final static String FILE_LOCATION = "C:/client/";
	
	private Socket socket;
	private ObjectOutputStream oos;
	private InputStream in;

	public GcFileClient() throws IOException {
		socket = new Socket(SERVER, SOCKET_PORT);
		System.out.println("Connecting...");
		
		File file = new File(FILE_LOCATION);
		file.mkdirs();
	}
	
	public void write(GcCommunicationModel model) {
		try {
			oos = new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(model);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String readFile(){
		DataInputStream clientData;
		BufferedOutputStream bos;
		DataOutputStream dos;
		OutputStream output;
		String fileName = "";
		int len;
		int smblen;
		boolean flag = true;
		try {
			while (flag == true) {
				in = socket.getInputStream();
				clientData = new DataInputStream(in);

				System.out.println("Starting...");

				int fileSize = clientData.read();

				ArrayList<File> files = new ArrayList<File>(fileSize); 
				ArrayList<Integer> sizes = new ArrayList<Integer>(fileSize);
				
				for (int count = 0; count < fileSize; count++) {
					File ff = new File(clientData.readUTF());
					files.add(ff);
				}

				for (int count = 0; count < fileSize; count++) {
					sizes.add(clientData.readInt());
				}

				for (int count = 0; count < fileSize; count++) {
					if (fileSize - count == 1) {
						flag = false;
					}

					len = sizes.get(count);

					System.out.println("File Size =" + len);

					output = new FileOutputStream(FILE_LOCATION + files.get(count));
					fileName = FILE_LOCATION + files.get(count);
					dos = new DataOutputStream(output);
					bos = new BufferedOutputStream(output);
					
					byte[] buffer = new byte[1024];
					bos.write(buffer, 0, buffer.length); 
					while (len > 0 && (smblen = clientData.read(buffer)) > 0) {
						dos.write(buffer, 0, smblen);
						len = len - smblen;
						dos.flush();
					}
					dos.close(); 
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return fileName;
	}

//	public static void main(String[] args) throws IOException {
//		List<String> file = new ArrayList<String>();
//		String n1 = "c:/server/1.png";
//		String n2 = "c:/server/2.jpg";
//		String n3 = "c:/server/3.jpg";
//		String n4 = "c:/server/4.jpg";
//		file.add(n1);
//		file.add(n2);
//		file.add(n3);
//		file.add(n4);
//		GcFileClient gc = new GcFileClient();
//	}
}
